﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model;

namespace Hazop
{
    public partial class ProjectDetails : Form{
        public static string projectName;
        private DataTable dtTree = null;
        private DataView dv = null;

        public ProjectDetails()
        {
            InitializeComponent();
            CenterToScreen();
            cbxHazopType.SelectedIndex = 0;
        }

        private void btnNext_Click(object sender, EventArgs e){
            if (tbxProjectName.Text.Equals("")){
                MessageBox.Show("项目名称未填写!");
                return;
            }
            projectName = tbxProjectName.Text;
            MainForm.currentProject = projectName;
            this.Close();
         //   CreateProjectTree();
        }

        private void CreateProjectTree(){
            CreateTable();
            CreateTree();
            this.Close();
        }

        private void CreateTable()
        {
            dtTree = new DataTable("ProjectTree");
            dtTree.Columns.Add(new DataColumn("TypeID", typeof(int)));
            dtTree.Columns.Add(new DataColumn("TypeName", typeof(string)));
            dtTree.Columns.Add(new DataColumn("TypeParentID", typeof(int)));

            dtTree.Rows.Add(new object[] { 1, projectName, 0 });
            dtTree.Rows.Add(new object[] { 2, "Hazop小组", 1 });
            dtTree.Rows.Add(new object[] { 3, "PSI清单", 1 });
            dtTree.Rows.Add(new object[] { 4, "图纸清单", 1 });
            dtTree.Rows.Add(new object[] { 5, "节点", 1 });
            dtTree.Rows.Add(new object[] { 6, "风险矩阵", 1 });
            dtTree.Rows.Add(new object[] { 7, "通用建议", 5 });
            
        }

        private void CreateTree()
        {
            dv = dtTree.DefaultView;
            dv.Sort = "TypeParentID ASC";
            DataRowView[] arrDRV = dv.FindRows(0);
            if (arrDRV.Length == 0)
                return;
            TreeNode tnNew = null;
            foreach (DataRowView drv in arrDRV)
            {
                tnNew = MainForm.mainForm.ProjectTree.Nodes.Add(drv.Row["TypeName"].ToString());
                tnNew.Tag = drv.Row["TypeID"].ToString();
                CreateTreeNode(ref tnNew);
            }
        }

        private void CreateTreeNode(ref TreeNode tnParent)
        {
            DataRowView[] arrDRV = dv.FindRows(tnParent.Tag);
            if (arrDRV.Length == 0)
                return;
            TreeNode tnNew = null;
            foreach (DataRowView drv in arrDRV)
            {
                tnNew = tnParent.Nodes.Add(drv.Row["TypeName"].ToString());
                tnNew.Tag = drv.Row["TypeID"].ToString();
                CreateTreeNode(ref tnNew);
            }
        }
    }
}
